package execute;
import java.util.HashMap;
import java.util.List;
import javax.swing.JFrame;

import knapsackProblem.KnapsackItem;


import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.view.mxGraph;

public class ShowIt extends JFrame {
	private static final long serialVersionUID = 1L;

	private mxGraph graph;
	private HashMap<KnapsackItem, Object> mapper = new HashMap<KnapsackItem, Object>();
	private double x, y;
	private final int inix = 50, dx = 100, dy = 50, w = 40, h = 20;

	public ShowIt() {
		super();
		graph = new mxGraph();
		x = inix;
		y = 10;
	}

	public void GenerateRoot(KnapsackItem root) {
		graph.getModel().beginUpdate();
		try {
			Object v = graph.insertVertex(graph.getDefaultParent(), "0",
					"INIT", x, y, w, h);
			mapper.put(root, v);
		} finally {
			graph.getModel().endUpdate();
		}
		mxGraphComponent graphComponent = new mxGraphComponent(graph);
		getContentPane().add(graphComponent);
	}

	public void AddChildren(KnapsackItem parent, List<KnapsackItem> children) {
		Object v1 = this.mapper.get(parent);

		y += dy;
		x = inix;

		for (KnapsackItem knapsackItem : children) {

			graph.getModel().beginUpdate();
			try {
				Object v2 = graph.insertVertex(graph.getDefaultParent(),
						Integer.toString(knapsackItem.getId()),
						knapsackItem.getProfit(), x, y, w, h);
				this.mapper.put(knapsackItem, v2);
				graph.insertEdge(graph.getDefaultParent(), null,
						knapsackItem.getTime(), v1, v2);
				x += dx;
			} finally {
				graph.getModel().endUpdate();
			}
			mxGraphComponent graphComponent = new mxGraphComponent(graph);
			getContentPane().add(graphComponent);
		}
	}
}
